<template>
  <div>
    <h3>借还历史记录</h3>
    <el-form :model="form" inline>
      <el-form-item label="借用时间">
        <el-date-picker
          v-model="form.borrowTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
          ></el-date-picker
        >
      </el-form-item>
      <el-form-item label="归还时间">
        <el-date-picker
          v-model="form.returnTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="借用人">
        <el-input
          v-model="form.borrowById"
          placeholder="请选择借用人"
        ></el-input>
      </el-form-item>
      <el-form-item label="工具编号">
        <el-input
          v-model="form.barcode"
          placeholder="请选择工具编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="飞机注册号">
        <el-select v-model="form.airplaneId" placeholder="请选择飞机注册号">
          <el-option
            v-for="item in airplane"
            :label="item.airplane"
            :value="item.airplane"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="submit">查 询</el-button>
    <el-button type="primary" @click="download">下 载</el-button>
    <el-button type="primary" @click="print">打 印</el-button>
    <div>
      <el-table :data="tableData">
        <el-table-column
          label="工具名称"
          prop="toolName"
          width="150px"
        ></el-table-column>
        <el-table-column label="工具编号" prop="barcode"></el-table-column>
        <el-table-column label="飞机注册号" prop="airplaneId"></el-table-column>
        <el-table-column
          label="借用时间"
          prop="borrowTime"
          width="200px"
        ></el-table-column>
        <el-table-column
          label="归还时间"
          prop="returnTime"
          width="200px"
        ></el-table-column>
        <el-table-column label="借用人" prop="borrowByName"></el-table-column>
        <el-table-column label="归还人" prop="returnByName"></el-table-column>
        <el-table-column label="操作者" prop="operatorName"></el-table-column>
        <el-table-column label="借用数量" prop="borrowCount"></el-table-column>
        <el-table-column label="归还数量" prop="returnCount"></el-table-column>
      </el-table>
    </div>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
import historyApi from "@/api/history/index";
import airplaneApi from "@/api/airplane/index";
import { verifyBarcode } from "@/utils/verify/verify.js";
export default {
  data() {
    return {
      tableData: [],
      form: {
        borrowTime: "",
        returnTime: "",
        borrowById: "",
        barcode: "",
        airplaneId: "",
      },
      airplane: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  methods: {
    /**
     * 打印用户的未归还记录，用于借完工具以后方便显示
     */
    print() {
      if (verifyBarcode(this.form.barcode, this)) {
        if (
          this.form.borrowTime ||
          this.form.returnTime ||
          this.form.borrowById ||
          this.form.barcode ||
          this.form.airplaneId
        ) {
          historyApi.printPage(
            this.form.borrowTime,
            this.form.returnTime,
            this.form.borrowById,
            this.form.barcode,
            this.form.airplaneId
          );
        } else {
          this.$message({
            message: "查询选项为空时不支持打印！",
            type: "error",
          });
        }
      }
    },
    download() {
      if (verifyBarcode(this.form.barcode, this)) {
        if (
          this.form.borrowTime ||
          this.form.returnTime ||
          this.form.borrowById ||
          this.form.barcode ||
          this.form.airplaneId
        ) {
          historyApi.download(
            this.form.borrowTime,
            this.form.returnTime,
            this.form.borrowById,
            this.form.barcode,
            this.form.airplaneId
          );
        }
      }
    },
    handleCurrentChange: async function (currentPage) {
      this.list(currentPage);
    },
    submit: async function () {
      if (
        !this.form.borrowTime &&
        !this.form.returnTime &&
        !this.form.borrowById &&
        !this.form.barcode &&
        !this.form.airplaneId
      ) {
        this.tableData = [];
        return;
      }
      this.list(this.currentPage);
      this.getTotal();
    },
    list: async function (currentPage) {
      // console.log("list", this.form, this.form.airplaneId);
      if (verifyBarcode(this.form.barcode, this)) {
        if (
          this.form.borrowTime ||
          this.form.returnTime ||
          this.form.borrowById ||
          this.form.barcode ||
          this.form.airplaneId
        ) {
          const res = await historyApi.list(
            this.form.borrowTime,
            this.form.returnTime,
            this.form.borrowById,
            this.form.barcode,
            this.form.airplaneId,
            currentPage,
            this.pageSize
          );
          this.tableData = res.data;
        }
      }
    },
    getTotal: async function () {
      if (verifyBarcode(this.form.barcode, this)) {
        if (
          this.form.borrowTime ||
          this.form.returnTime ||
          this.form.borrowById ||
          this.form.barcode ||
          this.form.airplaneId
        ) {
          const res = await historyApi.total(
            this.form.borrowTime,
            this.form.returnTime,
            this.form.borrowById,
            this.form.barcode,
            this.form.airplaneId
          );
          this.total = res.data;
        }
      }
    },
    getAirplane: async function () {
      airplaneApi.all().then((res) => {
        this.airplane = res.data;
      });
    },
  },
  created() {
    this.getAirplane();
  },
};
</script>

<style></style>
